package com.ruoyi.starbucks.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import lombok.extern.slf4j.Slf4j;

import java.text.MessageFormat;

@Slf4j
public class SrmService5 {
    public static String userName = "dbb0f807-14b6-4817-8444-7d85b39b8778";

    public static String password = "abc880918";

    public static String itemId = "459";

    public static String token = "";

    public static void Login()
    {
        String url = MessageFormat.format("http://api.bailongma123.com/api/yhdl?password={0}&apiAccount={1}",password,userName);
        String json = HttpUtils.sendGet(url,null);
        JSONObject jsonObject = (JSONObject)JSONObject.parse(json);
        String result = jsonObject.getString("result");
        if (result.equals("成功"))
        {
            token = jsonObject.getString("token");
            log.info("登录,token="+token);
        }
    }

    public static String GetPhoneNo() {
        if (token==null || token.isEmpty())
        {
            Login();
        }
        String url = MessageFormat.format("http://api.bailongma123.com/api/yhqh_s?token={0}&id={1}",token,itemId);
        String json = HttpUtils.sendGet(url,null);
        JSONObject jsonObject = (JSONObject)JSONObject.parse(json);
        String result = jsonObject.getString("result");
        if (result.equals("成功"))
        {
            String phoneNo = jsonObject.getString("number");
            return phoneNo;
        }
        return "";
    }

    public static String GetMessage(String phoneNo) {
        if (token==null || token.isEmpty())
        {
            Login();
        }
        String url = MessageFormat.format("http://api.bailongma123.com/api/yhjm?token={0}&id={1}&number={2}",token,itemId,phoneNo);
        String json = HttpUtils.sendGet(url,null);
        JSONObject jsonObject = (JSONObject)JSONObject.parse(json);
        String result = jsonObject.getString("result");
        if (result.equals("成功"))
        {
            String msg = jsonObject.getString("smsContent");
            if(StringUtils.isNotEmpty(msg)){
                log.info("获取到的短信:"+msg);
                return msg;
            }
        }
        return  MessageFormat.format("ERROR:尚未收到短信，请确保您已经向手机号{0}发送过短信",phoneNo);
    }

    public static String AddToBlackList(String phoneNo){
        if (token==null || token.isEmpty())
        {
            Login();
        }

        String url =MessageFormat.format("http://api.bailongma123.com/api/yhlh?token={0}&id={1}&number={2}&sent=1",token,itemId,phoneNo);
        String json = HttpUtils.sendGet(url,null);
        JSONObject jsonObject = (JSONObject)JSONObject.parse(json);
        String result = jsonObject.getString("result");
        if (result.equals("成功"))
        {
            return  MessageFormat.format("{0}号码拉黑成功",phoneNo);
        }
        return "ERROR:号码拉黑失败,请再试一次";
    }


    public static void main(String[] args) {
/*        *//**
        *登录
        *//*
          Login();
        *//**
         * 获取手机号码
         *//*
        String PhoneNo=  GetPhoneNo();*/

        String msg= GetMessage("15883072476");
        /**
         * 获取短信
         */
    /*    while (true){
           String msg= GetMessage("15883072476");
           System.out.println(msg);
        }*/

//        GetMessage("16533210620");

        /**
         * 加入黑名单
         */
//        AddToBlackList(GetPhoneNo());
    }
}
